package fschmidt.db.postgres;

import java.sql.Connection;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.SQLException;


public final class PostgresExceptionHandler {
	private boolean inTrans;
	private Connection pgCon;
	private Savepoint sp;

	public PostgresExceptionHandler(Statement stmt) throws SQLException {
		pgCon = stmt.getConnection();
		inTrans = !pgCon.getAutoCommit();
		if( inTrans )
			sp = pgCon.setSavepoint();
	}

	public void handleException() throws SQLException {
		if( inTrans )
			pgCon.rollback(sp);
	}

	public void close() throws SQLException {
		if( inTrans )
			pgCon.releaseSavepoint(sp);
	}

}
